package org.wlldTest.lesson7;

import org.dromara.easyai.randomForest.DataTable;
import org.dromara.easyai.randomForest.RandomForest;
import org.wlldTest.MySql.Sql;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Test {
    private static final RandomForest randomForest;
    private static List<String> list;

    static {
        try {
            randomForest = new RandomForest(9);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public Test() throws Exception {
    }

    public static void main(String[] args) throws Exception {
        study();
        test("我要找吃病人");
    }

    public static void test(String sentence) throws Exception {
        Word myWord = new Word();
        for (int i = 0; i < sentence.length(); i++) {
            if (i < 10) {
                int t = i + 1;
                int id = getIndex(list, sentence.substring(i, i + 1));
                String methodName = "setA" + t;
                Method method = myWord.getClass().getMethod(methodName, int.class);
                method.invoke(myWord, id);
            } else {
                break;
            }
        }
        int type = randomForest.forest(myWord);
        System.out.println("该语句类别为:" + type);
    }

    public static void study() throws Exception {
        List<Sentence> sentences = Sql.sql().getSql().selectList("selectMyWord");
        Set<String> mySet = new HashSet<>();
        for (int i = 1; i < 11; i++) {
            mySet.add("a" + i);
        }
        mySet.add("typeId");
        DataTable dataTable = new DataTable(mySet);
        dataTable.setKey("typeId");
        randomForest.init(dataTable);
        Set<String> wordSet = new HashSet<>();
        for (Sentence sentence : sentences) {
            String word = sentence.getWord();
            for (int i = 0; i < word.length(); i++) {
                wordSet.add(word.substring(i, i + 1));
            }
        }
        list = new ArrayList<>(wordSet);
        for (Sentence sentence : sentences) {
            String word = sentence.getWord();
            Word myWord = new Word();
            int typeId = sentence.getTypeId();
            for (int i = 0; i < word.length(); i++) {
                if (i < 10) {
                    int t = i + 1;
                    int id = getIndex(list, word.substring(i, i + 1));
                    String methodName = "setA" + t;
                    Method method = myWord.getClass().getMethod(methodName, int.class);
                    method.invoke(myWord, id);
                } else {
                    break;
                }
            }
            myWord.setTypeId(typeId);
            randomForest.insert(myWord);
        }
        randomForest.study();
    }

    public static int getIndex(List<String> list, String word) {
        int index = 0;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).equals(word)) {
                index = i + 1;
                break;
            }
        }
        return index;
    }
}
